package com.kejiguan.ui;

import android.app.Activity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.kejiguan.ui.adapter.AlarmNeAdapter;
import com.kejiguan.ui.event.RefreshNeStateEvent;
import com.kejiguan.ui.presenter.AlarmActivityPresenter;
import com.kejiguan.ui.response.GetNeItemInfosResponse;
import com.kejiguan.ui.util.CacheDataUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * Created by YYY on 16/9/2.
 */
public class ActivityAlarm extends Activity implements View.OnClickListener{

    private AlarmActivityPresenter mPresenter;
    private RecyclerView alarmList;
    private View backView;
    private String alarmcontent;
    private AlarmNeAdapter alarmNeAdapter;

    public void onCreate(Bundle bundle)
    {
        super.onCreate(bundle);
        getIntentData();
        setContentView(R.layout.activity_alarm_layout);
        mPresenter=new AlarmActivityPresenter();
        mPresenter.onCreate(ActivityAlarm.this);

        EventBus.getDefault().register(ActivityAlarm.this);
        initViews();

        mPresenter.getNeItemInfos();

    }

    private void getIntentData()
    {
        alarmcontent=getIntent().getStringExtra("alarmcontent");
    }

    private void initViews()
    {
        alarmList= (RecyclerView) findViewById(R.id.alarm_list);
        backView=findViewById(R.id.back_btn);
        backView.setOnClickListener(this);

        GridLayoutManager layoutManager=new GridLayoutManager(ActivityAlarm.this,2);
        alarmList.setLayoutManager(layoutManager);
        alarmNeAdapter=new AlarmNeAdapter(ActivityAlarm.this, null);
        alarmList.setAdapter(alarmNeAdapter);

    }

    @Override
    public void onDestroy()
    {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void onClick(View v) {

        switch (v.getId())
        {
            case R.id.back_btn:
                finish();
                break;
        }
    }



    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(GetNeItemInfosResponse response) {
        alarmNeAdapter.setExhibitionNeItems(CacheDataUtil.getInstance().getAlarmExhibitionNeItems(alarmcontent));
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(RefreshNeStateEvent event) {
        alarmNeAdapter.setExhibitionNeItems(CacheDataUtil.getInstance().getAlarmExhibitionNeItems(alarmcontent));
    }
}
